import numpy as np
from numpy.linalg import matrix_rank, pinv, solve

if __name__ == '__main__':
    A = np.array([
        [1, 3, 0, 2], [0, 0, 1, 4], [1, 3, 1, 6]
    ])

    b = np.array([1, 6, 7])

    rank = matrix_rank(A)

    # 系统矩阵为满秩的情况
    if rank == A.shape[0]:
        x = solve(A, b)
        print(x)

    else:
        A_pinv = pinv(A)
        x_p = A_pinv @ b
        # null_space = np.eye(A.shape[1]) - A_pinv @ A

        # 输出特解
        print(x_p)
        # 输出特殊解
        # print(null_space)
